1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.collect.MultimapBuilder.MultimapBuilderWithKeys;
22
23 import junit.framework.TestCase;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import java.math.RoundingMode;
30 import java.util.SortedMap;
31 import java.util.SortedSet;
32
33
34
35
36
37
38 @GwtCompatible(emulated = true)
39 public class MultimapBuilderTest extends TestCase {
40
41 @GwtIncompatible("doesn't build without explicit type parameters on build() methods")
42 public void testGenerics() {
43 ListMultimap<String, Integer> a = MultimapBuilder.hashKeys().arrayListValues().build();
44 SortedSetMultimap<String, Integer> b = MultimapBuilder.linkedHashKeys().treeSetValues().build();
45 SetMultimap<String, Integer> c =
46 MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER).hashSetValues().build();
47 }
48
49 public void testGenerics_gwtCompatible() {
50 ListMultimap<String, Integer> a =
51 MultimapBuilder.hashKeys().arrayListValues().<String, Integer>build();
52 SortedSetMultimap<String, Integer> b =
53 MultimapBuilder.linkedHashKeys().treeSetValues().<String, Integer>build();
54 SetMultimap<String, Integer> c = MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER)
55 .hashSetValues().<String, Integer>build();
56 }
57
58 @GwtIncompatible("doesn't build without explicit type parameters on build() methods")
59 public void testTreeKeys() {
60 ListMultimap<String, Integer> multimap = MultimapBuilder.treeKeys().arrayListValues().build();
61 assertTrue(multimap.keySet() instanceof SortedSet);
62 assertTrue(multimap.asMap() instanceof SortedMap);
63 }
64
65 public void testTreeKeys_gwtCompatible() {
66 ListMultimap<String, Integer> multimap =
67 MultimapBuilder.treeKeys().arrayListValues().<String, Integer>build();
68 assertTrue(multimap.keySet() instanceof SortedSet);
69 assertTrue(multimap.asMap() instanceof SortedMap);
70 }
71
72 @GwtIncompatible("serialization")
73 public void testSerialization() throws Exception {
74 for (MultimapBuilderWithKeys<?> builderWithKeys : ImmutableList.of(
75 MultimapBuilder.hashKeys(), MultimapBuilder.linkedHashKeys(), MultimapBuilder.treeKeys(),
76 MultimapBuilder.enumKeys(RoundingMode.class))) {
77 for (MultimapBuilder<?, ?> builder : ImmutableList.of(
78 builderWithKeys.arrayListValues(),
79 builderWithKeys.linkedListValues(),
80 builderWithKeys.hashSetValues(),
81 builderWithKeys.linkedHashSetValues(),
82 builderWithKeys.treeSetValues(),
83 builderWithKeys.enumSetValues(RoundingMode.class))) {
84
85
86
87 reserializeAndAssert(builder.build());
88 }
89 }
90 }
91
92 @GwtIncompatible("serialization")
93 private static void reserializeAndAssert(Object object) throws Exception {
94 Object copy = reserialize(object);
95 assertEquals(object, copy);
96 assertEquals(object.getClass(), copy.getClass());
97 }
98
99 @GwtIncompatible("serialization")
100 private static Object reserialize(Object object) throws Exception {
101 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
102 new ObjectOutputStream(bytes).writeObject(object);
103 return new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())).readObject();
104 }
105 }